package it.valeandre.omino.fragment;

import it.valeandre.omino.R;
import it.valeandre.omino.adapter.TimelineAdapter;
import it.valeandre.omino.model.Match;
import it.valeandre.omino.utils.Maps;
import it.valeandre.omino.utils.ResBundle;
import it.valeandre.omino.utils.TypedPair;
import it.valeandre.omino.utils.Utils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class MatchTimelinePageFragment extends BaseFragment {

    public static Fragment newInstance(Bundle matchData)
    {
        MatchTimelinePageFragment myFragment = new MatchTimelinePageFragment();
        myFragment.setArguments(matchData);
        return myFragment;
    }

    public static Fragment newInstance(Bundle matchData, OnFragmentTouchListener listener)
    {
        MatchTimelinePageFragment myFragment = new MatchTimelinePageFragment();
        myFragment.setArguments(matchData);
        myFragment.setOnFragmentTouchListener(listener);
        return myFragment;
    }

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		ViewGroup rootView = (ViewGroup) inflater.inflate(
                R.layout.fragment_match_detail_timeline, container, false);
        Bundle matchData = getArguments();
        Match match  = (Match)matchData.getSerializable(ResBundle.KEY_BUNDLE_MATCH_DETAIL_DATA);
        fillHeadData(rootView, match);

        /*TimelineView timeline = (TimelineView) rootView.findViewById(R.id.fragment_match_detail_timeline_view);
        timeline.fill(match);*/
        ListView timeline = (ListView) rootView.findViewById(R.id.fragment_match_detail_timeline_scroll_list);

        List<TypedPair<String, String>> events = getOrderedEventslist(match);
        TimelineAdapter timelineAdapter = new TimelineAdapter(getActivity(), events);
        timeline.setAdapter(timelineAdapter);

//        ScrollView scroll = (ScrollView) rootView.findViewById(R.id.fragment_match_detail_timeline_scroll);
        timeline.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if(mOnFragmentTouchListener != null) {
                    mOnFragmentTouchListener.onFragmentTouch(motionEvent);
                }
                return false;
            }
        });
        return rootView;
	}

    private List<TypedPair<String, String>> getOrderedEventslist(Match match) {

        List<TypedPair<String, String>> goalDetailsPairList = match.getHomeGoalDetailsPairList().getEventDetailPairList();
        List<TypedPair<String, String>> yellowCardsDetailsPairList = match.getHomeYellowCardsDetailsPairList().getEventDetailPairList();
        List<TypedPair<String, String>> redCardsDetailsPairList = match.getHomeRedCardsDetailsPairList().getEventDetailPairList();

        List<TypedPair<String, String>> list = new ArrayList<TypedPair<String, String>>();
        list.addAll(goalDetailsPairList);
        list.addAll(yellowCardsDetailsPairList);
        list.addAll(redCardsDetailsPairList);

        goalDetailsPairList = match.getAwayGoalDetailsPairList().getEventDetailPairList();
        yellowCardsDetailsPairList = match.getAwayYellowCardsDetailsPairList().getEventDetailPairList();
        redCardsDetailsPairList = match.getAwayRedCardsDetailsPairList().getEventDetailPairList();

        list.addAll(goalDetailsPairList);
        list.addAll(yellowCardsDetailsPairList);
        list.addAll(redCardsDetailsPairList);

        Collections.sort(list, new Comparator<TypedPair<String, String>>() {
            public int compare(TypedPair<String, String> o1, TypedPair<String, String> o2) {
                if(Integer.parseInt(o1.getMinute().replace("'", "").trim()) < Integer.parseInt(o2.getMinute().replace("'", "").trim())) {
                    return -1;
                } else if(Integer.parseInt(o1.getMinute().replace("'", "").trim()) > Integer.parseInt(o2.getMinute().replace("'", "").trim())) {
                    return 1;
                } else return 0;
            }
        });

        return list;
    }

    private void fillHeadData(ViewGroup rootView, Match match) {
        String hometeamString = match.getHometeam() == null || match.getHometeam().trim().equals("") ? match.getHometeamRealtime() : match.getHometeam();
        String visitorteamString = match.getAwayteam() == null || match.getAwayteam().trim().equals("") ? match.getAwayteamRealtime() : match.getAwayteam();

        TextView matchDay = (TextView) rootView.findViewById(R.id.fragment_match_detail_common_info_league_and_day);
        matchDay.setText(match.getLeague() + " " + getResources().getString(R.string.league_day) + " " + match.getRound());

        TextView matchDate = (TextView) rootView.findViewById(R.id.fragment_match_detail_common_info_date);
        String formattedDate = Utils.convertGmtStringToString(match.getDate(), "yyyy-MM-dd'T'hh:mm:ssZ", Locale.getDefault());
        matchDate.setText(formattedDate);

        ImageView homeTeamLogo = (ImageView) rootView.findViewById(R.id.fragment_match_detail_common_info_home_logo);
        Utils.setLogoByName(hometeamString, homeTeamLogo, Maps.LOGOS_DIMENSION_SMALL_ID);

        ImageView awayTeamLogo = (ImageView) rootView.findViewById(R.id.fragment_match_detail_common_info_away_logo);
        Utils.setLogoByName(visitorteamString, awayTeamLogo, Maps.LOGOS_DIMENSION_SMALL_ID);

        TextView homeTeamName = (TextView) rootView.findViewById(R.id.fragment_match_detail_common_info_home_team);
        homeTeamName.setText(hometeamString);

        TextView awayTeamName = (TextView) rootView.findViewById(R.id.fragment_match_detail_common_info_away_team);
        awayTeamName.setText(visitorteamString);

        TextView homeTeamScore = (TextView) rootView.findViewById(R.id.fragment_match_detail_common_info_home_score);
        homeTeamScore.setText(match.getHomeGoals());

        TextView awayTeamScore = (TextView) rootView.findViewById(R.id.fragment_match_detail_common_info_away_score);
        awayTeamScore.setText(match.getAwayGoals());
    }

}
